<?php

namespace app\admin\dao;

use app\common\model\MemberDraft;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * MemberDraftDao 数据访问对象
 * Class MemberDraftDao
 * @package app\dao
 * @method MemberDraft findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method MemberDraft insertActive(array $params, array $allow_field = [])
 * @method MemberDraft updateActive(MemberDraft $model, array $params, array $allow_field = [])
 * @method MemberDraft findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method MemberDraft updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(MemberDraft $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class MemberDraftDao
{
    use DaoTrait;

    /**
     * @var MemberDraft
     */
    private $baseModel;

    /**
     * MemberDraftDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = MemberDraft::class;
    }

    /**
     * 待审核律师咨询
     * @param $checkStatus
     * @param $provinceId
     * @param $cityId
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @param $subType
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function listLawyersDraft($checkStatus, $provinceId, $cityId, $startTime, $endTime, $kw, $subType, ?int $page, ?int $page_size): array
    {
        $tableName = MemberDraft::getTable();

        $query = MemberDraft::leftJoin('member m', "$tableName.memberId=m.id")
            ->where("$tableName.checkStatus", $checkStatus)
            ->where("$tableName.subType", $subType)
            ->when(is_id($provinceId), fn($query) => $query->where("$tableName.provinceId", $provinceId))
            ->when(is_id($cityId), fn($query) => $query->where("$tableName.cityId", $cityId))
            ->when(is_numeric($startTime), fn($query) => $query->where("$tableName.created", '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where("$tableName.created", '<=', $endTime))
            ->search("$tableName.realname|m.phone|$tableName.practicingLawFirm", $kw)
            ->tableField(true, $tableName)
            ->tableField(['followedWechat'], 'm')
            ->with([
                'member' => function ($query) {
                    $query->field(['id', 'nickname', 'realname', 'headImgUrl', 'cropImgUrl', 'phone', 'type'])
                        // ->with([
                        //     'inviteMember' => fn($query) => $query->field(['id', 'nickname', 'realname', 'headImgUrl', 'cropImgUrl', 'type',])
                        //         ->append(['headImg', 'headImgThumb', 'cropImg', 'showName']),
                        // ])
                        ->append(['headImg', 'headImgThumb', 'cropImg', 'showName'])
                        ->hidden(['headImgUrl']);
                },
                'province' => fn($query) => $query->shortArea(),
                'city' => fn($query) => $query->shortArea(),
                'categories' => fn($query) => $query->shortCategory(),
            ])
            ->append(['idcardPicImg', 'idcard2PicImg', 'certificatePicImg', 'headImg', 'cropImg', 'todo'])
            ->hidden(['idcardPic', 'idcard2Pic', 'certificatePic', 'headImgUrl'])
            ->order("$tableName.id", 'desc')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    public function statisticsListLawyersDraft($checkStatus, $provinceId, $cityId, $startTime, $endTime, $kw, $subType): array
    {
        $tableName = MemberDraft::getTable();
        $query = MemberDraft::leftJoin('member m', "$tableName.memberId=m.id")
            // ->where("$tableName.checkStatus", $checkStatus)
            ->where("$tableName.subType", $subType)
            ->when(is_id($provinceId), fn($query) => $query->where("$tableName.provinceId", $provinceId))
            ->when(is_id($cityId), fn($query) => $query->where("$tableName.cityId", $cityId))
            ->when(is_numeric($startTime), fn($query) => $query->where("$tableName.created", '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where("$tableName.created", '<=', $endTime))
            ->search("$tableName.realname|m.phone|$tableName.practicingLawFirm", $kw);

        return [
            'check0_count' => (clone $query)->where("$tableName.checkStatus", '0')->count(),
            'check2_count' => (clone $query)->where("$tableName.checkStatus", '2')->count(),
        ];
    }

}
